package com.lzw.bluetoothpractice.network;



import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Utils.DigestUtils;
import com.lzw.bluetoothpractice.Utils.MD5Utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class HeadAssmblyUtils {
    public static Map<String, String> HeadAssembly(String s) throws Exception {

        Map<String, String> map = new HashMap<>();
        String contentMD5 = MD5Utils.MD5To32LowerCase(s);
        long currentTime = System.currentTimeMillis() + AppConfig.TIME_DIFFERENCE;
        Date date = new Date(currentTime);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // 设置时区为GMT
        String gmtStr = sdf.format(date.getTime());
        StringBuilder stb = new StringBuilder();
        String content = stb.append("x-date: ").append(gmtStr).append("\n").append("content-md5: ").append(contentMD5).toString();
        String hmacsha256 = DigestUtils.HMACSHA256(content, AppConfig.HMAC_SERET_ADD);
        String hmacCode = String.format("hmac username=\"userhmac\", algorithm=\"hmac-sha256\", headers=\"x-date content-md5\", signature=\"%s\"", hmacsha256);
        map.put("contentMD5", contentMD5);
        map.put("gmtStr", gmtStr);
        map.put("hmacCode", hmacCode);
        return map;

    }
}
